package com.ruoyi.web.controller.drugSystem.drugInfo;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.drugSystem.domain.SysStockCk;
import com.ruoyi.drugSystem.domain.SysStockInfo;
import com.ruoyi.drugSystem.domain.SysStockRk;
import com.ruoyi.drugSystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 仓库库存信息Controller
 *
 * @author ruoyi
 * @date 2023-03-08
 */
@RestController
@RequestMapping("/system/info")
public class SysStockInfoController extends BaseController {
    @Autowired
    private ISysStockInfoService sysStockInfoService;
    @Autowired
    private ISysDrugInfoService sysDrugInfoService;
    @Autowired
    private ISysSupplierService sysSupplierService;
    @Autowired
    private ISysDrugStockService sysDrugStockService;
    @Autowired
    private ISysStockRkService sysStockRkService;
    @Autowired
    private ISysStockCkService sysStockCkService;
    /**
     * 查询仓库库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysStockInfo sysStockInfo) {
        startPage();
        List<SysStockInfo> list = sysStockInfoService.selectSysStockInfoList(sysStockInfo);
        return getDataTable(list);
    }

    /**
     * 导出仓库库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "仓库库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysStockInfo sysStockInfo) {
        List<SysStockInfo> list = sysStockInfoService.selectSysStockInfoList(sysStockInfo);
        ExcelUtil<SysStockInfo> util = new ExcelUtil<SysStockInfo>(SysStockInfo.class);
        util.exportExcel(response, list, "仓库库存信息数据");
    }

    /**
     * 获取仓库库存信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{stockInfoId}")
    public AjaxResult getInfo(@PathVariable("stockInfoId") Long stockInfoId) {
        return success(sysStockInfoService.selectSysStockInfoByStockInfoId(stockInfoId));
    }

    /**
     * 新增仓库库存信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "仓库库存信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysStockInfo sysStockInfo) {
        int i = sysStockInfoService.insertSysStockInfo(sysStockInfo);
        SysStockInfo info = sysStockInfoService.selectSysStockInfoByStockInfoId(sysStockInfo.getStockInfoId());
        System.out.println(info);
        SysStockRk sysStockRk = new SysStockRk();
        sysStockRk.setStockName(info.getSysDrugStock().getStockName());
        sysStockRk.setRugdetailedinfoName(info.getSysDrugInfo().getDrugdetailedinfoName());
        sysStockRk.setDrugclassName(info.getSysDrugInfo().getSysDrugClass().getDrugClassName());
        sysStockRk.setBrandName(info.getSysDrugInfo().getSysDrugBrand().getBrandName());
        sysStockRk.setSupplierName(info.getSysSupplier().getSuppplierName());
        sysStockRk.setNumber(info.getNumber());
        sysStockRk.setCreateTime(info.getCreateTime());
        sysStockRkService.insertSysStockRk(sysStockRk);
        return toAjax(i);
    }

    /**
     * 修改仓库库存信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "仓库库存信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysStockInfo sysStockInfo) {
        return toAjax(sysStockInfoService.updateSysStockInfo(sysStockInfo));
    }

    /**
     * 删除仓库库存信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "仓库库存信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stockInfoIds}/{num}")
    public AjaxResult remove(@PathVariable Long[] stockInfoIds, @PathVariable Long num) {
        SysStockInfo info = sysStockInfoService.selectSysStockInfoByStockInfoId(stockInfoIds[0]);
        SysStockCk sysStockCk = new SysStockCk();
        sysStockCk.setStockName(info.getSysDrugStock().getStockName());
        sysStockCk.setRugdetailedinfoName(info.getSysDrugInfo().getDrugdetailedinfoName());
        sysStockCk.setDrugclassName(info.getSysDrugInfo().getSysDrugClass().getDrugClassName());
        sysStockCk.setBrandName(info.getSysDrugInfo().getSysDrugBrand().getBrandName());
        sysStockCk.setSupplierName(info.getSysSupplier().getSuppplierName());
//        sysStockCk.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        sysStockCk.setNumber(num);
        sysStockCkService.insertSysStockCk(sysStockCk);
        Long num1 = info.getNumber() - num;
        if (num1 != 0) {
            info.setNumber(num1);
            sysStockInfoService.updateSysStockInfo(info);
            return success(num1);
        } else {
            return toAjax(sysStockInfoService.deleteSysStockInfoByStockInfoIds(stockInfoIds));
        }


    }

    @GetMapping("/getAll")
    public AjaxResult getAll() {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("infoOptions",sysDrugInfoService.selectDrugInfoAll());
        ajax.put("supplierOptions",sysSupplierService.selectSysSupplierList(null));
        ajax.put("stockOptions",sysDrugStockService.selectSysDrugStockList(null));
        return ajax;
    }
}
